/*
    SDL - Simple DirectMedia Layer
    Partial implementation of SDL library (originally written by
    Sam Lantinga <slouken@libsdl.org>) for the particular purpose to support
    Prequengine (http://code.google.com/p/prequengine/) on BlackBerry(tm)
    tablets and smartphones.

    Copyright (C) 2013  xlamsp

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

    xlamsp@gmail.com
*/

#ifndef SDL_BBVIDEO_H_
#define SDL_BBVIDEO_H_

#include "SDL_events.h"
#include "SDL_config.h"

#define VKBD_BUTTON_UP          (0)
#define VKBD_BUTTON_DOWN        (1)

#define TOUCH_ID_MAX            (8)
#define MARGIN_WIDTH            (5)     /* pixels */

/* physical keyboard scan codes */

    /* first row */
#define SCAN_CODE_Q             (0x14)
#define SCAN_CODE_W             (0x1a)
#define SCAN_CODE_E             (0x08)
#define SCAN_CODE_R             (0x15)
#define SCAN_CODE_T             (0x17)
#define SCAN_CODE_Y             (0x1c)
#define SCAN_CODE_U             (0x18)
#define SCAN_CODE_I             (0x0c)
#define SCAN_CODE_O             (0x12)
#define SCAN_CODE_P             (0x13)

    /* second row */
#define SCAN_CODE_A             (0x04)
#define SCAN_CODE_S             (0x16)
#define SCAN_CODE_D             (0x07)
#define SCAN_CODE_F             (0x09)
#define SCAN_CODE_G             (0x0a)
#define SCAN_CODE_H             (0x0b)
#define SCAN_CODE_J             (0x0d)
#define SCAN_CODE_K             (0x0e)
#define SCAN_CODE_L             (0x0f)
#define SCAN_CODE_DEL           (0x2a)

    /* third row */
#define SCAN_CODE_ALT           (0xe2)
#define SCAN_CODE_Z             (0x1d)
#define SCAN_CODE_X             (0x1b)
#define SCAN_CODE_C             (0x06)
#define SCAN_CODE_V             (0x19)
#define SCAN_CODE_B             (0x05)
#define SCAN_CODE_N             (0x11)
#define SCAN_CODE_M             (0x10)
#define SCAN_CODE_DOLLAR        (0x21)
#define SCAN_CODE_ENTER         (0x28)

    /* fourth row */
#define SCAN_CODE_LSHIFT        (0xe1)
#define SCAN_CODE_ZERO          (0x27)
#define SCAN_CODE_SPACE         (0x2c)
#define SCAN_CODE_SYM           (0x71)
#define SCAN_CODE_RSHIFT        (0xe5)


typedef enum {
    E_KEY_MIN = 0,

    E_KEY_ESC = E_KEY_MIN,
    E_KEY_SPACE,
    E_KEY_ENTER,
    E_KEY_SHIFT,
    E_KEY_ALT,
    E_KEY_CTRL,
    E_KEY_F1,
    E_KEY_F2,
    E_KEY_F3,
    E_KEY_F4,
    E_KEY_P,
    E_KEY_H,
    E_KEY_UP,
    E_KEY_DOWN,
    E_KEY_LEFT,
    E_KEY_RIGHT,

    E_KEY_TOTAL
} key_e;

typedef enum {
    E_HIT_EVENT_POINTER = (1 << 0),
    E_HIT_ACCELEROMETER = (1 << 1)
} key_hit_e;

typedef enum {
    E_X_INVALID,
    E_X_LEFT,
    E_X_RIGHT
} app_win_x_pos_e;

typedef enum {
    E_Y_INVALID,
    E_Y_UP,
    E_Y_MID,
    E_Y_DOWN
} app_win_y_pos_e;

typedef struct {
    SDLKey sym;
    char *img_path[2];
} key_descr_t;

typedef struct {
    screen_window_t win;
    screen_buffer_t buf[2];
    int pos[2];
    int size[4];
    key_hit_e hit;
    int hit_cntr;
    SDLKey sym;
    char *img_path[2];
} vkbd_key_t;

typedef struct {
    app_win_x_pos_e x_pos;
    app_win_y_pos_e y_pos;
    key_e key;
} app_win_key_t;

typedef struct {
    int edge_x_mid;
    int edge_y_top;
    int edge_y_btm;
    app_win_key_t touch_scr[TOUCH_ID_MAX];
    app_win_key_t pointer;
} app_win_kbd_t;

typedef struct {
    screen_window_t win;
    screen_buffer_t buf[2]; /* double buffering */
    int cur_buf;
    int upd_pend;           /* number of pending updates */
    pthread_cond_t cond;
    pthread_mutex_t mutex;
    pthread_t thread;
    int thread_quit;

    app_win_kbd_t kbd;
} app_window_t;

typedef struct {
    int head;
    int tail;
    int queue_size;
    SDL_Event *queue;
    pthread_mutex_t mutex;
    pthread_t thread;
    int thread_quit;
} events_t;

typedef struct {
    int enable_accelerometer;
} config_t;

/* Private display data */
typedef struct SDL_PrivateVideoData {
    screen_context_t screen_ctx;
    screen_window_t screen_win;
    char screen_group_id[16];
    int screen_w;
    int screen_h;
    int screen_rotation;

    app_window_t app_win;
    events_t events;
    vkbd_key_t keyboard[E_KEY_TOTAL];

    config_t config;
} SDL_PrivateVideoData;

typedef struct private_hwdata
{
    screen_pixmap_t pixmap;
    screen_buffer_t buffer;
} private_hwdata;

#endif /* SDL_BBVIDEO_H_ */

